import routeConfig from './routeConfig'
import dva from 'dva'
import counter from './models/counter'
import students from './models/students'
import { createBrowserHistory } from 'history'

// 得到一个dva对象
const app = dva({
    history: createBrowserHistory(),
    initialState: { counter: 56 },
    onError(err, dispatch) { },
    // 可以配置 redux 中间件
    onAction: store => next => action => {
        console.log('action', action);
        next(action)
    },
    onStateChange(state) {
        console.log(state.counter);
    },
    onReducer(reducer) {
        // reducer 原来在模型里面配置的
        // 传入一个reducer 返回一个reducer
        return function (state, action) {
            console.log(reducer.name);
            return reducer(state, action)
        }
    },
    // 当副作用发生的时候
    onEffect(oldEffect, sagaEffect, model, actionType) {
        console.log(oldEffect);
        return function* (action) {
            yield oldEffect(action)
        }
    },
    extraReducers: {
        abc(state = 123, action) {
            return state
        },
        bcd(state = 123, action) {
            return state
        }
    },
    extraEnhancers: [function (createStore) {
        return function (...args) {
            console.log('即将创建仓库1');
            return createStore(...args)
        }
    }, function (createStore) {
        return function (...args) {
            console.log('即将创建仓库2');
            return createStore(...args)
        }
    }]
})

// 在启动之前定义模型
app.model(counter)
app.model(students)

// 设置根路由，即启动后，要运行的函数，函数的返回结果会被渲染
// app.router(() => <App />)
app.router(routeConfig) // App是一个函数组件

app.start('#root')